<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;


class Source extends Backend
{
     /**
     * 当前控制器下的一个模型属性
    */
    protected $model = null;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        // 引入的模型
        $this->model = model('Business.Source');
    }

    public function index()
    {
        //设置过滤方法
        //去掉参数中所有的html部分
        $this->request->filter(['strip_tags','trim']);

        // Backend 后台基类控制器中提供的一个获取查询条件的参数方法
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) 
        {
            $params = $this->request->post('row/a');   //以数组的方式存在
            if ($params) 
            {   

                //添加
                $result = $this->model->validate("common/Business/Source")->save($params);

                //失败情况
                if ($result === false) 
                {
                    $this->error($this->model->getError());
                    exit;
                }

                $this->success();
                exit;
            }
        }

        return $this->view->fetch();
    }

    public function edit($ids = null)
    {   

        //查询当前点击的数据是否存在
        $row = $this->model->find($ids);

        if (!$row) 
        {
            $this->error(__('No results were found'));
            exit;
        }

        if ($this->request->isPost()) 
        {
            $params = $this->request->post("row/a");

            if ($params) 
            {
                $params['id'] = $ids;
                
                $result = $this->model->validate("common/Business/Source")->isUpdate(true)->save($params);

                if ($result === false) 
                {
                    $this->error($this->model->getError());
                    exit;
                }

                $this->success();
                exit;
            }
        }

        //为模板赋值
        $this->assign("row", $row);

        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $ids = !empty($ids) ?  explode(',', $ids) : [];

        //用select 查询多条数据，删除单条数据也是删除多条数据情况中的一种
        $row = $this->model->select($ids);

        if(!$row)
        {
            $this->error(__('No result were found'));
            exit;
        }
        
        //执行删除语句
        $result = $this->model->destroy($ids);

        if ($result === false) 
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
        exit;

    }
}
